//
//  JWTUpdateSignatureViewController.m
//  JWT
//
//  Created by ndm on 17/1/22.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTUpdateSignatureViewController.h"


static NSString *max_length = @"30";

@interface JWTUpdateSignatureViewController ()<UITextViewDelegate>
{
    //修改姓名的textfiled
    UITextView *_updateSignTextView;
    //确认修改
    UIButton *_rightBtn;
    //
    UILabel *_stirngLenghLabel;
    
}

@end

@implementation JWTUpdateSignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithRightBtn];
    
    [self initWithAppearance];
}

-(void) initWithRightBtn{
    //保存按钮
    if (_rightBtn == nil) {
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 40, 30)];
        [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [rightBtn setTitleColor:RGB_WhiteBackGround forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(updateSignatureAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        self.navigationItem.rightBarButtonItem = rightBarItem;
        self.navigationController.navigationBar.tintColor = RGB(46, 46, 46);
        _rightBtn = rightBtn;
    }
}

-(void) updateSignatureAction:(UIButton *)sender{
    if ([_updateSignTextView.text isEqualToString:APPDELEGATE.userInfo.signature] || [_updateSignTextView.text isEqualToString:@""]) {
        [HUD showToast:@"个性签名未更改,请重置"];
        return;
    }else if([_updateSignTextView.text length] > [max_length integerValue]) {
        [HUD showToast:@"长度不能超30,请重置"];
        return;
    } else{
        APPDELEGATE.userInfo.signature = _updateSignTextView.text;
        
        //修改个性签名
        [self requestDataWithUpdateSignature];
    }
}

-(void)requestDataWithUpdateSignature{
    [HUD showUIBlockingIndicator];
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] updateUserSignWithActionUpdateSign];
    NSDictionary *dict = @{@"userid":APPDELEGATE.userInfo.userid,@"signature":APPDELEGATE.userInfo.signature,@"tokenid":APPDELEGATE.userInfo.tokenid};
    [AFNHttpsRequest requestWithUrl:urlString WithParam:dict WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"] integerValue] == 200) {
                [HUD showToast:[model objectForKey:@"message"]];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh_updateUserInfoAction" object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [HUD showToast:[model objectForKey:@"message"]];
            }
        }
    }];
}

-(void) initWithAppearance{
    if (_updateSignTextView == nil) {
        UITextView *updateSignTextView = [[UITextView alloc] init];
        updateSignTextView.frame = CGRectMake(self.view.left+2*Cell_Margin,  self.view.top+3*Cell_Margin, UISCREEN_WIDTH-4*Cell_Margin, 88);
        updateSignTextView.layer.borderWidth = 0.5;
        updateSignTextView.layer.borderColor = RGB_LightGray.CGColor;
        updateSignTextView.layer.cornerRadius = updateSignTextView.height*0.08;
        updateSignTextView.backgroundColor = RGB_WhiteBackGround;
        updateSignTextView.font = Text_Font14;
        updateSignTextView.delegate = self;
        [self.view addSubview:updateSignTextView];
        _updateSignTextView = updateSignTextView;
        _updateSignTextView.text = APPDELEGATE.userInfo.signature;
    }
    
    _stirngLenghLabel = [[UILabel alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH-150, _updateSignTextView.bottom, 120, 34)];
    _stirngLenghLabel.font = Text_Font13;
    _stirngLenghLabel.textColor = RGB_DarkGray;
    _stirngLenghLabel.text = @"0/30";
    _stirngLenghLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_stirngLenghLabel];
    
}

//正在改变
- (void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"%@", textView.text);
//    _rightBtn.enabled = YES;
    //实时显示字数
    _stirngLenghLabel.text = [NSString stringWithFormat:@"%lu/30", (unsigned long)textView.text.length];
    
    //字数限制操作
    if (textView.text.length >= [max_length integerValue]) {
        
        textView.text = [textView.text substringToIndex:[max_length integerValue]];
        _stirngLenghLabel.text = @"30/30";
        
    }
    //取消按钮点击权限，并显示提示文字
//    if (textView.text.length == 0) {
//        _rightBtn.enabled = NO;
//    }
}

@end
